let app = getApp();
Component({
  properties: {
    propArray: {
      type: Array,
      observer: function(newValue, oldValue) {
        // console.log('needauth-oldValue', oldValue)
        // console.log('needauth-newValue', newValue)
        // console.log("propArray")
        // 监控到新数据
        // this.setData({
        //   items: newValue
        // })
        // 回调
        this.dataInit(newValue)
      }
    }
  },
  data: {
    all: {
      id: "全选",
      val: "全选",
      checked: false
    },
    items: [],
    transmit_id: [], // 传给后台的数组
  },
  lifetimes: {
    attached() {},
  },
  methods: {
    dataInit: function(e) {
      this.setData({
        items: e
      })
    },
    checkboxChange: function(e) { //全选
      let t_items = [];
      this.data.transmit_id = [];
      if (e.detail.value == this.data.all.id) {
        for (let i = 0; i < this.data.items.length; i++) {
          t_items[i] = {
            id: this.data.items[i].id,
            val: this.data.items[i].val,
            checked: true
          };
          this.data.transmit_id.push(this.data.items[i].id)
        }
        this.setData({
          items: t_items
        });
        this.triggerEvent('allcheckbox_get', this.data.transmit_id)
      } else {
        for (let i = 0; i < this.data.items.length; i++) {
          t_items[i] = {
            id: this.data.items[i].id,
            val: this.data.items[i].val,
            checked: false
          };
        }
        this.setData({
          items: t_items,
          transmit_id: []
        });
        this.triggerEvent('allcheckbox_get', this.data.transmit_id)
      };
    },

    checkboxSingleChange: function(e) { //单选
      // console.log("单选");
      if (e.detail.value.length == this.data.items.length) {
        this.setData({
          ['all.checked']: true
        })
      } else {
        this.setData({
          ['all.checked']: false
        })
      }
      this.data.transmit_id = e.detail.value;
      this.triggerEvent('allcheckbox_get', this.data.transmit_id)
    },
  }
})
